package server;

import com.google.gson.Gson;
import dao.*;
import imp.*;
import model.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class AndroidServer extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String msg = request.getParameter("msg");
        switch (msg) {
            case "showgoods":
                showgoods(request, response, out);
                break;
            case "insertgoods":
                insertgoods(request, response, out);
                break;
            case "updategoods":
                updategoods(request, response, out);
                break;
            case "deletegoods":
                deletegoods(request, response, out);
                break;
            case "registered":
                registered(request, response, out);
                break;
            case "showgoodsinfo":
                showgoodsinfo(request, response, out);
                break;
            case "login":
                login(request, response, out);
                break;
            case "showcategorygoods":
                showcategorygoods(request, response, out);
                break;
            case "showserachgoods":
                showserachgoods(request, response, out);
                break;
            case "addcomments":
                addcomments(request, response, out);
                break;
            case "showcomments":
                showcomments(request, response, out);
                break;
            case "updateuser":
                updateuser(request, response, out);
                break;
            case "showusergoods":
                showusergoods(request, response, out);
                break;
            case "addinformagainstinfo":
                addinformagainstinfo(request, response, out);
                break;
            case "showuserinformAgainst":
                showuserinformAgainst(request, response, out);
                break;
            case "showtrading":
                showtrading(request, response, out);
                break;
            case "addtrading":
                addtrading(request, response, out);
                break;
            case "updatetrading":
                updatetrading(request, response, out);
                break;
            case "deletetrading":
                deletetrading(request, response, out);
                break;
            case "judgetradinginfo":
                judgetradinginfo(request, response, out);
                break;
            case "judgetjubaoinfo":
                judgetjubaoinfo(request, response, out);
                break;
            case "getuserphone":
                getuserphone(request, response, out);
                break;
            case "serachrecord":
                serachrecord(request, response, out);
                break;
            case "addserachrecord":
                addserachrecord(request, response, out);
                break;
            case "webshowgoods":
                webshowgoods(request, response, out);
                break;
            case "webdeletegoods":
                webdeletegoods(request, response, out);
                break;
            case "adminlogin":
                adminlogin(request, response, out);
                break;
            case "adminexit":
                adminexit(request, response, out);
                break;
            case "webshowjubao":
                webshowjubao(request, response, out);
                break;
            case "webshenhejubao":
                webshenhejubao(request, response, out);
                break;
            case "getuserintegral":
                getuserintegral(request, response, out);
                break;
            case "addcollection":
                addcollection(request, response, out);
                break;
            case "showcollection":
                showcollection(request, response, out);
                break;
            case "deletecollection":
                deletecollection(request, response, out);
                break;
            case "judgecollection":
                judgecollection(request, response, out);
                break;
            case "judgetrading":
                judgetrading(request, response, out);
                break;
            case "judgetradingok":
                judgetradingok(request, response, out);
                break;
            default:
                break;
        }
    }

    private void getuserphone(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/android?msg=getuserphone&userid=1
        String userid = request.getParameter("userid");
        UserDao userDao = new User_imp();
        User user = new User();
        user = userDao.GetUserPhone(userid);
        Gson gson = new Gson();
        out.println(gson.toJson(user));
    }
    private void getuserintegral(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/android?msg=getuserintegral&userid=1
        String userid = request.getParameter("userid");
        UserDao userDao = new User_imp();
        User user = new User();
        user.setIntegral(userDao.GetUserIntegral(userid));
        Gson gson = new Gson();
        out.println(gson.toJson(user));
    }


    //交易记录显示
    private void showtrading(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/android?msg=showtrading&userid=1
        String userid = request.getParameter("userid");
        TradingDao tradingDao = new Trading_imp();
        List<Trading> list;
        list = tradingDao.ShowTrading(userid);
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }

    //判断是否正在进行交易中
    private void judgetradinginfo(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/android?msg=judgetradinginfo&goodsid=1
        String goodsid = request.getParameter("goodsid");
        TradingDao tradingDao = new Trading_imp();
        Trading trading = new Trading();
        trading.setId(null);
        trading = tradingDao.judgetradinginfo(goodsid);
        if (trading.getId() == null) {
            out.println("true");//不在交易中
        }
    }

    //判断是否重复举报
    private void judgetjubaoinfo(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/android?msg=judgetjubaoinfo&goodsid=1&userid=2
        String goodsid = request.getParameter("goodsid");
        String userid = request.getParameter("userid");
        InformAgainstDao informAgainstDao = new InformAgainst_imp();
        InformAgainst informAgainst = new InformAgainst();
        informAgainst.setId(null);
        informAgainst = informAgainstDao.judgeinfo(goodsid, userid);
        if (informAgainst.getId() == null) {
            out.println("true");//不存在举报信息
        }
    }
    //判断交易是否已被取消
    private void judgetrading(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/android?msg=judgetjubaoinfo&goodsid=1&userid=2
        String tradingid = request.getParameter("id");
        TradingDao tradingDao = new Trading_imp();
        Trading trading = new Trading();
        trading.setId(null);
        trading = tradingDao.judgetrading(tradingid);
        if (trading.getId() == null) {
            out.println("true");
        }
    }
    //判断交易是否已完成
    private void judgetradingok(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/androidserver?msg=judgetradingok&id=1
        String tradingid = request.getParameter("id");
        TradingDao tradingDao = new Trading_imp();
        Trading trading = new Trading();
        trading.setId(null);
        trading = tradingDao.judgetrading(tradingid);
        if (trading.getState().equals("交易完成")) {
            out.println("true");
        }
    }
    //交易记录添加
    private void addtrading(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/android?msg=addtrading&sellerid=1&buyerid=2.1&goodsid=4
        String sellerid = request.getParameter("sellerid");
        String buyerid = request.getParameter("buyerid");
        String goodsid = request.getParameter("goodsid");
        Date now = new Date();
        DateFormat d1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        String str1 = d1.format(now);
        UUID id = UUID.randomUUID();
        Trading trading = new Trading();
        trading.setId(String.valueOf(id));
        trading.setSellerId(sellerid);
        trading.setBuyerId(buyerid);
        trading.setGoodsId(goodsid);
        trading.setDate(str1);
        trading.setState("交易中");
        TradingDao tradingDao = new Trading_imp();
        if (tradingDao.InsertTrading(trading)) {
            out.println("true");
        }
    }

    //交易完成
    private void updatetrading(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //        http://localhost:8080/android?msg=updatetrading&tradingid=1&state=交易完成&goodsid=2
        String tradingid = request.getParameter("tradingid");
        String state = request.getParameter("state");
        String goodsid = request.getParameter("goodsid");
//        Trading trading = new Trading();
//        trading.setId(tradingid);
//        trading.setState(state);
        GoodsDao goodsDao = new Goods_imp();
        TradingDao tradingDao = new Trading_imp();
        if (tradingDao.UpdateTrading(tradingid, state) && goodsDao.UpdateTradingFinishGoods(goodsid)) {
            out.println("true");
        }
    }

    //交易记录删除
    private void deletetrading(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/test?msg=deletetrading&tradingid=1
        String tradingid = request.getParameter("tradingid");
        TradingDao tradingDao = new Trading_imp();
        if (tradingDao.DeleteTrading(tradingid)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //显示单个物品信息
    private void showgoodsinfo(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/android?msg=showgoodsinfo&goodsid=1
        String goodsid = request.getParameter("goodsid");
        GoodsDao goodsinfo = new Goods_imp();
        Goods goods = goodsinfo.ShowGoodsInfo(goodsid);
        Gson gson = new Gson();
        out.println(gson.toJson(goods));
    }

    //添加搜索记录
    private void addserachrecord(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/android?msg=addserachrecord&content=1
        String content = request.getParameter("content");
        UUID id = UUID.randomUUID();
        SearchRecordDao searchRecordDao = new SearchRecord_imp();
        if (searchRecordDao.select(content)) {
            if (searchRecordDao.add(String.valueOf(id), content)) {
                out.println("true");
            } else {
                out.println("false");
            }
        } else {
            if (searchRecordDao.addcount(content)) {
                out.println("true");
            } else {
                out.println("false");
            }
        }
    }

    //返回搜索记录
    private void serachrecord(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/android?msg=serachrecord
        List<String> list = new ArrayList();
        SearchRecordDao searchRecordDao = new SearchRecord_imp();
        list = searchRecordDao.returncontent();
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }

    //显示所有物品信息
    private void showgoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/androidserver?msg=showgoods
        GoodsDao goods = new Goods_imp();
        List<Goods> list;
        list = goods.ShowGoods();
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }

    //根据分类显示物品信息
    private void showcategorygoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/android?msg=showcategorygoods&category=
        String goods_category = request.getParameter("category");
        GoodsDao goods = new Goods_imp();
        List<Goods> list;
        list = goods.ShowGoods(goods_category);
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }

    //根据用户ID显示物品信息
    private void showusergoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/android?msg=showusergoods&user_id=
        String user_id = request.getParameter("user_id");
        GoodsDao goods = new Goods_imp();
        List<Goods> list;
        list = goods.ShowUserGoods(user_id);
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }

    //根据搜索显示物品信息
    private void showserachgoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/android?msg=showserachgoods&serach=
        String goods_serach = request.getParameter("serach");
        GoodsDao goods = new Goods_imp();
        List<Goods> list;
        list = goods.ShowSerachGoods(goods_serach);
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }

    //修改已发布的物品信息
    private void updategoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/test?msg=updategoods&goodsid=1&goodsname=2&goodscontent=3&goodscategory=4
        String goodsid = request.getParameter("goodsid");
        String goodsname = request.getParameter("goodsname");
        String goodsprice = request.getParameter("goodsprice");
        String goodscontent = request.getParameter("goodscontent");
        String goodsimgurl = request.getParameter("goodsimgurl");
        Goods goods = new Goods();
        goods.setId(goodsid);
        goods.setName(goodsname);
        goods.setPrice(goodsprice);
        goods.setContent(goodscontent);
        goods.setImgUrl(goodsimgurl);
        GoodsDao adminDao = new Goods_imp();
        if (adminDao.UpdateGoods(goods)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //删除已发布的物品
    private void deletegoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/androidserver?msg=deletegoods&goodsid=1
        String goodsid = request.getParameter("goodsid");
        GoodsDao adminDao = new Goods_imp();
        if (adminDao.DeleteGoods(goodsid)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //显示评论
    private void showcomments(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/android?msg=showcomments&goodsid=1
        String comments_goodsid = request.getParameter("goodsid");
        CommentsDao commentsDao = new Comments_imp();
        List<Comments> list;
        list = commentsDao.ShowComments(comments_goodsid);
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }
    //判断是否重复收藏
    private void judgecollection(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //      http://localhost:8080/android?msg=judgecollection&goodsid=1&userid=2
        String goodsid = request.getParameter("goodsid");
        String userid = request.getParameter("userid");
        CollectionDao collectionDao = new Collection_imp();
        Collection collection = new Collection();
        collection.setId(null);
        collection = collectionDao.judgeinfo(goodsid, userid);
        if (collection.getId() == null) {
            out.println("true");//不存在举报信息
        }
    }
    //添加收藏
    private void addcollection(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/android?msg=addcollection
        String collection_userid = request.getParameter("userid");
        String collection_goodsid = request.getParameter("goodsid");
        Date now = new Date();
        DateFormat d1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        String str1 = d1.format(now);
        UUID id = UUID.randomUUID();
        Collection collection = new Collection();
        collection.setId(String.valueOf(id));
        collection.setUserid(collection_userid);
        collection.setGoodsid(collection_goodsid);
        collection.setDate(str1);
        CollectionDao collectionDao = new Collection_imp();
        if (collectionDao.AddCollection(collection)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }
    //删除收藏
    private void deletecollection(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/androidserver?msg=deletecollection&id=1
        String id = request.getParameter("id");
        CollectionDao collectionDao = new Collection_imp();
        if (collectionDao.DeleteCollection(id)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //显示收藏
    private void showcollection(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/androidserver?msg=showcollection&goodsid=1
        String id = request.getParameter("id");
        System.out.println("调用到");
        CollectionDao collectionDao = new Collection_imp();
        List<Collection> list;
        list = collectionDao.ShowCollection(id);
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }
    //发布新评论
    private void addcomments(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/android?msg=addcomments&user_id=1&user_name=2&user_userimg=2.1&goods_id=3&content=4
        String comments_userid = request.getParameter("user_id");
        String comments_username = request.getParameter("user_name");
        String comments_userimg = request.getParameter("user_img");
        String comments_goodsid = request.getParameter("goods_id");
        String comments_content = request.getParameter("content");
        Date now = new Date();
        DateFormat d1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        String str1 = d1.format(now);
//        System.out.println(str1);
        UUID id = UUID.randomUUID();
        Comments comments = new Comments();
        comments.setId(String.valueOf(id));
        comments.setUserId(comments_userid);
        comments.setUserImgUrl(comments_userimg);
        comments.setUserName(comments_username);
        comments.setGoodsId(comments_goodsid);
        comments.setDate(str1);
        comments.setContent(comments_content);
        CommentsDao commentsDao = new Comments_imp();
        if (commentsDao.AddComments(comments)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //添加举报信息
    private void addinformagainstinfo(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//        http://localhost:8080/android?msg=addinformagainstinfo&goods_id=1&user_id=2&goods_name=2.1&goods_imgurl=3
        String goods_id = request.getParameter("goods_id");
        String user_id = request.getParameter("user_id");
        String goods_name = request.getParameter("goods_name");
        String goods_imgurl = request.getParameter("goods_imgurl");
        Date now = new Date();
        DateFormat d1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        String str1 = d1.format(now);
//        System.out.println(str1);
        UUID id = UUID.randomUUID();
        InformAgainst informAgainst = new InformAgainst();
        informAgainst.setId(String.valueOf(id));
        informAgainst.setDate(str1);
        informAgainst.setGoodsId(goods_id);
        informAgainst.setUserId(user_id);
        informAgainst.setGoodsName(goods_name);
        informAgainst.setGoodsImgUrl(goods_imgurl);
        informAgainst.setState("未处理");
        InformAgainstDao informAgainstDao = new InformAgainst_imp();
        if (informAgainstDao.AddInformAgainstInfo(informAgainst)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //根据用户ID显示举报信息
    private void showuserinformAgainst(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/android?msg=showuserinformAgainst&user_id=
        String user_id = request.getParameter("user_id");
        InformAgainstDao informAgainstDao = new InformAgainst_imp();
        List<InformAgainst> list;
        list = informAgainstDao.ShowInformAgainstInfo(user_id);
        Gson gson = new Gson();
        out.println(gson.toJson(list));
    }

    //发布新的物品
    //http://localhost:8080/android?msg=insertgoods&goods_user_id=1&goods_name=2&goods_imgurl=3&goods_date=4&goods_price=4&goods_content=4&goods_state=4&goods_category=4
    private void insertgoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        String goods_user_id = request.getParameter("goods_user_id");
        String goods_name = request.getParameter("goods_name");
        String goods_imgurl = request.getParameter("goods_imgurl");
        String goods_date = request.getParameter("goods_date");
        String goods_price = request.getParameter("goods_price");
        String goods_content = request.getParameter("goods_content");
        String goods_state = request.getParameter("goods_state");
        String goods_category = request.getParameter("goods_category");

//        Date now = new Date();
//        DateFormat d1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
//        String str1 = d1.format(now);
//        //随机生成
//        for (int i=0;i<100;i++){
//
//            UUID id = UUID.randomUUID();
//            System.out.println(String.valueOf(id));
//            Goods newgoods = new Goods();
//            newgoods.setId(String.valueOf(id));
//            newgoods.setUserId("aamir"+i);
//            newgoods.setName("aamir"+i);
//            newgoods.setImgUrl("aamir"+i);
//            newgoods.setDate(str1);
//            newgoods.setPrice("1"+i);
//            newgoods.setContent("aamir"+i);
//            newgoods.setState("true");
//            newgoods.setCategory("aamir"+i);
//            GoodsDao adminDao = new Goods_imp();
//            adminDao.InsertGoods(newgoods);
//        }

        UUID id = UUID.randomUUID();
        System.out.println(String.valueOf(id));
        Goods newgoods = new Goods();
        newgoods.setId(String.valueOf(id));
        newgoods.setUserId(goods_user_id);
        newgoods.setName(goods_name);
        newgoods.setImgUrl(goods_imgurl);
        newgoods.setDate(goods_date);
        newgoods.setPrice(goods_price);
        newgoods.setContent(goods_content);
        newgoods.setState(goods_state);
        newgoods.setCategory(goods_category);
        GoodsDao adminDao = new Goods_imp();
        if (adminDao.InsertGoods(newgoods)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request, response);
    }

    //添加用户注册信息
    //http://localhost:8080/android?msg=registered&user_account=1&user_name=2&user_password=2&user_phone=2&user_email=2&user_imgurl=2
    private void registered(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        String user_account = request.getParameter("user_account");
        String user_name = request.getParameter("user_name");
        String user_password = request.getParameter("user_password");
        String user_phone = request.getParameter("user_phone");
        String user_email = request.getParameter("user_email");
        String user_imgurl = request.getParameter("user_imgurl");
        UUID id = UUID.randomUUID();
        System.out.println(String.valueOf(id));
        System.out.println("账号:" + user_account + "用户名:" + user_name +
                "密码:" + user_password + "电话:" + user_phone +
                "email:" + user_email + "imgurl:" + user_imgurl);
        //生成用户200个
//        for (int i=0;i<200;i++){
//            UserDao user1 = new User_imp();
//            UUID user_id = UUID.randomUUID();
//            String account="aamir"+i;
//            String name="aamir"+i;
//            String password="aamir"+i;
//            String phone="1307633635";
//            String email="aamir"+i+"@qq.com";
//            String imgurl="http://linyusen.oss-cn-shenzhen.aliyuncs.com/UserPicture/da09470c-b06d-4a3a-9954-b31e9061cc41.jpg";
//            user1.registered(String.valueOf(user_id), account, name, password, phone
//                , email, imgurl);
//        }
        UserDao user1 = new User_imp();
        if (user1.registered(String.valueOf(id), user_account, user_name, user_password, user_phone
                , user_email, user_imgurl)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //修改用户个人信息
    private void updateuser(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
//      http://localhost:8080/androidserver?msg=updateuser&userid=1&username=2&userpasword=3&userimgurl=4&userphone=5&useremail=7
        String userid = request.getParameter("userid");
        String username = request.getParameter("username");
        String userpasword = request.getParameter("userpasword");
        String userimgurl = request.getParameter("userimgurl");
        String userphone = request.getParameter("userphone");
        String useremail = request.getParameter("useremail");
        User user = new User();
        user.setId(userid);
        user.setName(username);
        user.setPassword(userpasword);
        user.setImgurl(userimgurl);
        user.setPhone(userphone);
        user.setEmail(useremail);
        UserDao userDao = new User_imp();
        System.out.println("执行用户修改");
        if (userDao.UpdateUser(user)) {
            out.println("true");
        } else {
            out.println("false");
        }
    }

    //用户登录
    //http://localhost:8080/android?msg=login&user_account=1&user_password=2
    private void login(HttpServletRequest request,
                       HttpServletResponse response, PrintWriter out) {
        String user_account = request.getParameter("user_account");
        String user_password = request.getParameter("user_password");
        System.out.println("用户账号:" + user_account + "用户密码:" + user_password);
        UserDao userDao = new User_imp();
        User user = userDao.login(user_account, user_password);
        if (user != null) {
            Gson gson = new Gson();
            out.println(gson.toJson(user));
        } else {
            Gson gson = new Gson();
            User user1 = new User();
            user1.setId("");
            user1.setAccount("");
            user1.setName("");
            user1.setPassword("");
            user1.setPhone("");
            user1.setEmail("");
            user1.setImgurl("");
            out.println(gson.toJson(user1));
//            out.println("false");
        }
    }


    //web部分开始
    //管理员登录
    private void adminlogin(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws IOException, ServletException {
//        out = response.getWriter();
//        out.write(request.getParameter("account"));
//        out.write("\r\n");
//        out.write(request.getParameter("password"));
//        out.write("\r\n");
        String account=request.getParameter("account");
        String password=request.getParameter("account");
        AdminDao adminDao=new Admin_imp();
        if (adminDao.login(account,password)) {
            RequestDispatcher dispatcher = request.getRequestDispatcher("/androidserver?msg=webshowgoods");
            dispatcher.forward(request, response);
        } else {
            RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
            dispatcher.forward(request, response);
        }
    }

    //管理员退出
    private void adminexit(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws IOException, ServletException {
//        out = response.getWriter();
//        out.write(request.getParameter("account"));
//        out.write("\r\n");
//        out.write(request.getParameter("password"));
//        out.write("\r\n");
        RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
        dispatcher.forward(request, response);

    }

    //显示物品部分
    private void webshowgoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws IOException, ServletException {
//        String p = request.getParameter("page");
//        int page;
//        try {
//            //当前页数
//            page = Integer.valueOf(p);
//        } catch (NumberFormatException e) {
//            page = 1;
//        }
//        System.out.println("页数" + p);
        List<Goods> goodsList;
        GoodsDao goodsDao = new Goods_imp();
        goodsList = goodsDao.ShowGoods();
        System.out.println("goodsList的" + goodsList.size());
//        GoodsDao goods = new Goods_imp();
//        List<Goods> list;
//        list = goods.ShowGoods();
//        System.out.println("list的"+list.size());
//        //物品总数
//        int goodssize = goodsList.size();
//        //每页显示物品数量
//        int goodsPerPage = 10;
//        //总页数
//        int PagesSize = goodssize % goodsPerPage == 0 ? goodssize / goodsPerPage : goodssize / goodsPerPage + 1;
//        //本页起始物品序号
//        int beginIndex = (page - 1) * goodsPerPage;
//        //本页末尾物品序号的下一个
//        int endIndex = beginIndex + goodsPerPage;
//        if (endIndex > goodssize)
//            endIndex = goodssize;

//        request.setAttribute("goodssize", goodssize);
//        request.setAttribute("goodsPerPage", goodsPerPage);
//        request.setAttribute("PagesSize", PagesSize);
//        request.setAttribute("beginIndex", beginIndex);
//        request.setAttribute("endIndex", endIndex);
//        request.setAttribute("page", page);
        request.setAttribute("goodsList", goodsList);
        request.getRequestDispatcher("home.jsp").forward(request, response);
    }

    //删除已发布的物品
    private void webdeletegoods(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws ServletException, IOException {
//        http://localhost:8080/androidserver?msg=deletegoods&goodsid=1
        String goodsid = request.getParameter("goodsid");
        GoodsDao adminDao = new Goods_imp();
        CommentsDao commentsDao=new Comments_imp();
        TradingDao tradingDao=new Trading_imp();
        if (adminDao.DeleteGoods(goodsid)&&commentsDao.DeleteComments(goodsid)&&tradingDao.DeleteGoodsTrading(goodsid)) {
            out.print("<script language='JavaScript'>alert('删除成功');</script>");
        } else {
            out.print("<script language='JavaScript'>alert('删除失败');</script>");
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("/androidserver?msg=webshowgoods");
        dispatcher.forward(request, response);
    }

    //显示举报部分
    private void webshowjubao(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws IOException, ServletException {
        InformAgainstDao informAgainstDao = new InformAgainst_imp();
        List<InformAgainst> list;
        list = informAgainstDao.ShowInformAgainstInfo();

//        List<Goods> goodsList;
//        GoodsDao goodsDao = new Goods_imp();
//        goodsList = goodsDao.ShowGoods();
//        System.out.println("goodsList的" + goodsList.size());
        request.setAttribute("jubaolist", list);
        request.getRequestDispatcher("jubao.jsp").forward(request, response);
    }

    //审核举报
    private void webshenhejubao(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws ServletException, IOException {
//        http://localhost:8080/androidserver?msg=webshenhejubao&jubaoid=1
        String jubaoid = request.getParameter("jubaoid");
        String goodsid = request.getParameter("goodsid");
        InformAgainstDao informAgainstDao=new InformAgainst_imp();
        GoodsDao goodsDao=new Goods_imp();
        String userid=goodsDao.GetGoodsUserId(goodsid);
        UserDao userDao=new User_imp();
        if (informAgainstDao.UpdateInformAgainst(jubaoid)&&userDao.ReduceUserIntegral(userid)&&goodsDao.UpdateTradingFinishGoods(goodsid)) {
            out.print("<script language='JavaScript'>alert('审核成功');</script>");
        } else {
            out.print("<script language='JavaScript'>alert('审核失败');</script>");
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("/androidserver?msg=webshowjubao");
        dispatcher.forward(request, response);
    }

}
